package fab.files
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	public class Select extends EventDispatcher
	{
		static public const LOADED:String = "loaded";
		private var fileRef:FileReference;
		public var file:Loader;
		public function Select()
		{
			fileRef = new FileReference();
			fileRef.addEventListener(Event.SELECT, selectHandler);
            fileRef.addEventListener(Event.COMPLETE, completeHandler);
            fileRef.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            fileRef.addEventListener(Event.OPEN, openHandler);				
			fileRef.browse(getTypes());
		}	
		private function getTypes():Array {
			var allTypes:Array = new Array(getImageTypeFilter());// , getTextTypeFilter());
			return allTypes;
		}		
		private function getImageTypeFilter():FileFilter {
			return new FileFilter("Images (*.jpg, *.jpeg, *.jpeg, *.gif, *.png)", "*.swf;*.jpg;*.jpeg;*.gif;*.png;*.SWF;.JPG;*.JPEG;*.GIF;*.PNG;");
		}
		private function getTextTypeFilter():FileFilter {
			return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");
		}		
		private function selectHandler(event:Event):void {
			fileRef.load();
		}
		private function securityErrorHandler(event:SecurityErrorEvent):void {
            //trace("securityErrorHandler: " + event);
        }		
        private function ioErrorHandler(event:IOErrorEvent):void {
            //trace("ioErrorHandler: " + event);
        }
        private function openHandler(event:Event):void {
            //trace("openHandler: " + event);
        }	
		private function completeHandler(event:Event):void {
            //trace("completeHandler: " + event); 
			file = new Loader();
			file.loadBytes(event.currentTarget.data);
			dispatchEvent( new Event( LOADED ) );
        }
	}
}